/**
  ******************************************************************************
  * @file    Bsp/Adc.c 
  * @author  zlf
  * @version V1.0.0
  * @date    2016-02-20
  * @brief   Adc.c module
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

	#include "Adc.h"
	
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address		( 0x4001244C )

/* Private macro -------------------------------------------------------------*/
vu16 eAdcSampleValue[ADC_DMA_NUM];

/* Private function prototypes -----------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  This function to initialise Adc gpio and Dma.
  * @param  None
  * @retval None
  */ 
void Adc_Init( void )
{
	ADC_InitTypeDef ADC_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	ADC_DeInit(ADC_USED);

	RCC_ADCCLKConfig(RCC_PCLK2_Div8);
	RCC_APB2PeriphClockCmd(ADC_CLOCK, ENABLE);
	RCC_AHBPeriphClockCmd(DMA_ADC_CLOCK , ENABLE);

	GPIO_InitStructure.GPIO_Pin = ADC_PIN_0 | ADC_PIN_1 | ADC_PIN_2 | ADC_PIN_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	
	GPIO_Init(ADC_GPIO, &GPIO_InitStructure);

	DMA_DeInit(DMA_ADC_CHANNEL);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)ADC1_DR_Address;
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32)eAdcSampleValue;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_BufferSize = ADC_DMA_NUM;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA_ADC_CHANNEL, &DMA_InitStructure);

	DMA_Cmd(DMA_ADC_CHANNEL, ENABLE);
 
	//Configure ADC
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
	ADC_InitStructure.ADC_ScanConvMode = ENABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfChannel = ADC_DMA_NUM;
	ADC_Init( ADC_USED, &ADC_InitStructure ); 

	ADC_RegularChannelConfig( ADC_USED, ADC_CHANNEL_LEVEL_0, 1, ADC_SampleTime_71Cycles5 );
	ADC_RegularChannelConfig( ADC_USED, ADC_CHANNEL_LEVEL_1, 2, ADC_SampleTime_71Cycles5 );
	ADC_RegularChannelConfig( ADC_USED, ADC_CHANNEL_LEVEL_2, 3, ADC_SampleTime_71Cycles5 );
	ADC_RegularChannelConfig( ADC_USED, ADC_CHANNEL_LEVEL_3, 4, ADC_SampleTime_71Cycles5 );
	
	ADC_DMACmd(ADC_USED, ENABLE);
	ADC_Cmd(ADC_USED, ENABLE); 
	
	ADC_ResetCalibration(ADC_USED); //Enable ADC1 Reset calibration register	
	while( ADC_GetResetCalibrationStatus(ADC_USED)){} //Check the end of ADC reset calibration register
	ADC_StartCalibration(ADC_USED);
    while( ADC_GetCalibrationStatus(ADC_USED)){}
	
	ADC_SoftwareStartConvCmd( ADC_USED, ENABLE ); //Start ADC software conversion
}



